#include "node_struct.h"

struct Tree
{
	bool rec;
	int cur;
	cudaNode* nodes;

	__device__ bool Tree::RecurseLeft()
	{
		rec = false;
		if(threadIdx.x == 0 && nodes[cur].left != cur && !nodes[cur].recurseLeft)
		{
			nodes[cur].recurseLeft = true;
			cur = nodes[cur].left;
			rec = true;
		}
		__syncthreads();
		return rec;
	}

	__device__ bool Tree::RecurseRight()
	{
		rec = false;
		if(threadIdx.x == 0 && nodes[cur].right != cur && !nodes[cur].recurseRight)
		{
			nodes[cur].recurseRight = true;
			cur = nodes[cur].right;
			rec = true;
		}
		__syncthreads();
		return rec;
	}

	__device__ void Tree::ResetRecursion()
	{
		if(threadIdx.x == 0 && nodes[cur].recurseLeft && nodes[cur].recurseRight)
		{
			nodes[cur].recurseLeft = false;
			nodes[cur].recurseRight = false;
		}
	}

	__device__ bool Tree::NextState()
	{
		rec = false;
		if(threadIdx.x == 0 && nodes[cur].state != 3)
		{
			nodes[cur].state++;
			rec = true;
		}
		__syncthreads();
		return rec;
	}

	__device__ bool Tree::IsCompleted()
	{
		rec = true;
		if(threadIdx.x == 0 && nodes[cur].state == 3 && nodes[cur].parent != cur)
		{
			nodes[cur].state = 0;
			cur = nodes[cur].parent;
			rec = false;
		}
		__syncthreads();
		return rec;
	}

};